//#ifndef __V4L_H__
//#define __V4L_H__
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <linux/fb.h>

#include <time.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


#include <unistd.h>  
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/videodev.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#include <errno.h>
#include <inttypes.h>

#include <stdint.h>
#include "misc_cpp.h"


// Defined Error Code in here
#ifndef ERRCODE
#define ERRCODE int32_t
#endif

typedef struct{
	unsigned int u32RemainBufSize;
	unsigned int u32RemainBufPhyAdr;
}S_BUF_INFO;

typedef struct 
{
	int32_t i32PipeBufNo;
	int32_t i32PipeBufSize;
	int32_t i32CurrPipePhyAddr;
}S_PIPE_INFO;

#define V4L_ERR					0x80400000							
#define ERR_V4L_SUCCESS				0
#define ERR_V4L_OPEN_DEV			(V4L_ERR | 0x1)
#define ERR_V4L_VID_GET_CAP			(V4L_ERR | 0x2)
#define ERR_V4L_VID_GRAB_DEV			(V4L_ERR | 0x3)
#define ERR_V4L_VID_SET_PICT			(V4L_ERR | 0x4)
#define ERR_V4L_VID_SET_WIN			(V4L_ERR | 0x5)
#define ERR_AVDEV_VID_CAPTURE			(V4L_ERR | 0x6)
#define ERR_V4L_MMAP				(V4L_ERR | 0x7)
#define ERR_V4L_VID_MCAPTURE			(V4L_ERR | 0x8)
#define ERR_V4L_VID_SYNC			(V4L_ERR | 0x9)
#define ERR_V4L_VID_CAPTURE			(V4L_ERR | 0xA)
#define ERR_V4L_VID_QUERY_UID		(V4L_ERR | 0xB)


/* Sensor CTRL */
#define REG_SDE_CTRL		0x6A
#define REG_BRIGHT		    0x9B


#define VIDIOCGCAPTIME				_IOR('v',30,struct v4l2_buffer)      /*Get Capture time */
#define VIDIOCSPREVIEW  			_IOR('v',43, int)
#define VIDIOC_QUERY_SENSOR_ID		_IOR('v',51, int)
#define	VIDIOC_G_BUFINFO			_IOR('v',52, S_BUF_INFO)

#define VIDIOC_G_PACKET_INFO		_IOR('v',53, S_PIPE_INFO)			/* Get Packet offset address and size */ 
#define VIDIOC_G_PLANAR_INFO		_IOR('v',54, S_PIPE_INFO)			/* Get Planar offset address and size */ 
#define VIDIOC_S_MAP_BUF			_IOW('v',55, int)					/* Specified the mapping buffer */

//fb ioctl
#define IOCTL_LCD_ENABLE_INT		_IO('v', 28)
#define IOCTL_LCD_DISABLE_INT		_IO('v', 29)


#define VIDEO_START	0
#define VIDEO_STOP	1

#if 0
typedef enum
{
	eIMAGE_QVGA,
	eIMAGE_WQVGA,
	eIMAGE_VGA,
	eIMAGE_SVGA,
	eIMAGE_HD720
}E_IMAGE_RESOL;

#endif
#define MAX_USER_CTRL 	16
typedef struct 
{    
	int32_t i32VidFD;    
	int32_t i32FrameFmt; /* see VIDEO_PALETTE_xxx */   
	int32_t i32UseMMap;   
	int32_t i32FrameSize;   
	struct video_window sVidWin;    
	uint8_t *pu8VidBuf;    
	struct video_mbuf sVidMBufs; // gb_buffers;    
	struct video_mmap sVidMMap;  //gb_buf;    
	int32_t i32VidFrame;         //gb_frame;
} S_V4L_VID_DATA;

class camera_v4l
{
private:
	S_V4L_VID_DATA s_sVidData;
	uint64_t s_u64PrevTS;
	string device_name;
	
	unsigned int ctrl_id;
	struct v4l2_queryctrl PrivateUserCtrl[MAX_USER_CTRL];


public:

	camera_v4l();
	camera_v4l(string name);

	
	ERRCODE InitV4LDevice(E_IMAGE_RESOL eImageResol);
	ERRCODE ReadV4LPicture( uint8_t **ppu8PicPtr,uint64_t *pu64TimeStamp,uint32_t *pu32BufPhyAddr);
	ERRCODE TriggerV4LNextFrame(void);
	
	void FinializeV4LDevice();
	
	ERRCODE StartV4LCapture(void);
	
	ERRCODE StopV4LCapture(void);
	
	ERRCODE SetV4LViewWindow(int lcmwidth, int lcmheight, int prewidth, int preheight);
	
	ERRCODE SetV4LEncode(int frame, int width, int height, int palette);
	
	ERRCODE StartPreview(void);
	ERRCODE StopPreview(void);
	ERRCODE QueryV4LUserControl(void);
	ERRCODE ChangeV4LUserControl_Brigness(int32_t ctrl);
	ERRCODE QueryV4LZoomInfo(struct v4l2_cropcap *psVideoCropCap,struct v4l2_crop *psVideoCrop);
	ERRCODE QueryV4LSensorID(int32_t* pi32SensorId);
	
	ERRCODE Zooming(struct v4l2_cropcap *psVideoCropCap,struct v4l2_crop *psVideoCrop,int32_t ctrl);
	
	ERRCODE MotionDetV4LSetThreshold(uint32_t u32Threshold);
	ERRCODE MotionDetV4LDumpDiffBuffer(void);
	
	ERRCODE QueryV4LPacketInformation(S_PIPE_INFO* ps_packetInfo);
	ERRCODE QueryV4LPlanarInformation(S_PIPE_INFO* ps_planarInfo);
	void menuChangeParameter(int i32FBFd,E_IMAGE_RESOL* pePreviewImgResol,E_IMAGE_RESOL* peEncodeImgResol);
	
	int dumpV4LBuffer(void);

	struct v4l2_queryctrl* ctrl_by_name( const char* name);


private:
	
	void menuEncodeV4LResoultion(E_IMAGE_RESOL* peEncodeImgResol);
	
	int32_t ChangeEncodeV4LResoultion(int32_t* pi32_width, int32_t* pi32_height);

};
#if 0

#endif




//#endif /*__V4L_H__*/


